
#include <iostream>
#include <string>
#include <map>
#include <list>
#include <stdlib.h>

#include "log.h"
#include "eventhandlerdata.h"
#include "pin.h"
#include "player.h"
#include "helpers.h"

#include <wiringPi.h>

using namespace std;

typedef void (*PlayerAction)(void); // function pointer type
typedef std::map<std::string, PlayerAction> ActionsMap;
ActionsMap actionsMap;


void makeAction(string);

void onPin7StateChanged(EventHandlerData ehdata){
    Pin* p = reinterpret_cast<Pin*>(ehdata.sourceObject);
    if(p->getState() == HIGH) {
        Log::Message("Play Action"); 
        playlist_play();
    } else{
        Log::Message("Pause Action");
        playlist_pause();
    }

}

void onPin0StateChanged(EventHandlerData ehdata){
    Pin* p = reinterpret_cast<Pin*>(ehdata.sourceObject);

    if(p->getState() == HIGH) {
        Log::Message("Play Action");
        playlist_next();
    }
}


void onPin27StateChanged(EventHandlerData ehdata){
    Pin* p = reinterpret_cast<Pin*>(ehdata.sourceObject);
    if(p->getState() == HIGH) {
        Log::Message("Select Radio");
        playlist_load_radios();
    }
}

void onPin28StateChanged(EventHandlerData ehdata){
    Pin* p = reinterpret_cast<Pin*>(ehdata.sourceObject);
    if(p->getState() == HIGH) {
        Log::Message("Select HDD");
        playlist_load_mp3();
    }
}

void onPin29StateChanged(EventHandlerData ehdata){
    Pin* p = reinterpret_cast<Pin*>(ehdata.sourceObject);
    if(p->getState() == HIGH) {
        Log::Message("Select AirPort");
        shairport_on();
    }
}


void onPin2_3StateChanged(EventHandlerData ehdata){
    int step = Pin::getRotationDirection(Pin::getPin(2),Pin::getPin(3))*2;
    stepVolume(step);
}

void initMPD(){
    setVolume(70);
}


int main() {

    initMPD();

    Pin::declarePinUse(7,INPUT)->registerEventHandler("stateChanged",  onPin7StateChanged);
    Pin::declarePinUse(0,INPUT)->registerEventHandler("stateChanged",  onPin0StateChanged);
    Pin::declarePinUse(27,INPUT)->registerEventHandler("stateChanged",  onPin27StateChanged);
    Pin::declarePinUse(28,INPUT)->registerEventHandler("stateChanged",  onPin28StateChanged);
    Pin::declarePinUse(29,INPUT)->registerEventHandler("stateChanged",  onPin29StateChanged);

    Pin::declarePinUse(2,INPUT)->registerEventHandler("stateChanged",  onPin2_3StateChanged);
    Pin::declarePinUse(3,INPUT)->registerEventHandler("stateChanged",  onPin2_3StateChanged);

    Pin::startPinWatcher();

    string action;
    for(;;)
    {
        cout << "bla :" << endl;
        cin>> action;
        delay(100);
    }


    return 0;
}

